package com.gitee.mrj.aiboot.core.sf.generators.ui;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.SFLayoutSpan;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFLayoutSpanGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFLayoutSpan span = AnnotatedElementUtils.findMergedAnnotation(field, SFLayoutSpan.class);
        generate(node, span);
    }

    @Override
    public void generate(UIForm node, Class clazz) {
        SFLayoutSpan span = AnnotatedElementUtils.findMergedAnnotation(clazz, SFLayoutSpan.class);
        generate(node, span);
    }

    private void generate(UIForm node, SFLayoutSpan span) {
        ObjectNode ui = node.getUi();
        ObjectNode uiGrid = putUiGrid(ui);
        uiGrid.put("span", span.value());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFLayoutSpan.class};
    }
}
